package com.shengyuan.manage_os.old.ying.erpm.controls;

import java.io.File;
import com.shengyuan.manage_os.old.ying.erpm.MainApplication;
import com.shengyuan.manage_os.old.ying.erpm.util.Utility;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.webkit.MimeTypeMap;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


public class VideoButton extends LinearLayout implements OnClickListener {
	
	public static String VideoFileName;
	private LayoutParams PlayerBtnLayoutParams,DeleteBtnLayoutParams,TitleLayoutParams;
	public Button player, delete;
	public TextView title;
	public String FILENAME = "video.3gp";
	private String FileName;
	private final int ID_PLAYER = 21,  ID_DELETE = 22, ID_TITLE = 23;
	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		case ID_PLAYER:
			//ECApplication.currentActivity.pHandler.sendEmptyMessage(ID_PLAYER);
			PlayVideo(VideoFileName);break;
//			Intent intent= new Intent();
//			Bundle bundle = new Bundle();
//			intent.putExtra("path", VideoFileName);
//			intent.setClass(ECApplication.currentActivity, PlayerVideo.class);
//			ECApplication.currentActivity.startActivity(intent);
		case ID_DELETE:
			Utility.delFile(FileName);
			getFileName();
			break;
		}
	}
    public void PlayVideo(String sFileName) {
		try {
			File file = new File(sFileName);
			if (file.exists()) {
				String extension = MimeTypeMap.getFileExtensionFromUrl(sFileName);  
				String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);  
				Intent intent = new Intent(Intent.ACTION_VIEW);
				//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
				//intent.putExtra("oneshot", 0);  
				//intent.putExtra("configchange", 0);  
				intent.setDataAndType(Uri.fromFile(file), mimeType);  
				MainApplication.currentActivity.startActivity(intent);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public VideoButton(Context context) {
		super(context);
		init(context);
	}

	public VideoButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public static boolean IsExist() {
		File file = new File(VideoFileName);
		return file.exists();
	}
	public String getFileName()
	{
		String f= MainApplication.voicePath + FILENAME;
		File file = new File(f);
		if (file.exists()) {
			title.setText("已经录像");
		}else{
			title.setText("");
			title.setHint("点按录像");
		}
		return f;
	}

	private void init(Context context) {
		player = new Button(context);
		delete = new Button(context);
		title = new TextView(context);
		player.setId(ID_PLAYER);
		delete.setId(ID_DELETE);
		title.setId(ID_TITLE);
		PlayerBtnLayoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT);
		DeleteBtnLayoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT);
		TitleLayoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT);
		DeleteBtnLayoutParams.weight =  PlayerBtnLayoutParams.weight = 1;
		TitleLayoutParams.weight= 5;
		
		addView(title, TitleLayoutParams);
		addView(player , PlayerBtnLayoutParams);
		addView(delete, DeleteBtnLayoutParams);
		title.setText("");
		title.setTextSize(18.0f);
		title.setTextColor(Color.BLUE);
		title.setEllipsize(TruncateAt.MIDDLE);  
		title.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);  
		title.setSingleLine(true);  
		player.setText("播放");
		delete.setText("删除");
		
		player.setOnClickListener(this);
		delete.setOnClickListener(this);
		FileName = getFileName();
		VideoFileName = FileName;
	}

	@Override
	public void postInvalidate() {
		super.postInvalidate();
		FileName = getFileName();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (FileName == null)
			return false;
		int action = event.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			break;
		case MotionEvent.ACTION_UP:
			VideoFileName = FileName;
			MainApplication.currentActivity.videoMethod();
			break;
		case MotionEvent.ACTION_CANCEL:
			break;
		}
		return true;
	}
}
